﻿using System;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace _072.EFCore多对多
{
	internal class Program
	{
		static async Task Main(string[] args)
		{
			// 数据插入
			//using (MyDbContext ctx = new MyDbContext())
			//{
			//	Student s1 = new Student() { Name = "张三" };
			//	Student s2 = new Student() { Name = "李四" };
			//	Student s3 = new Student() { Name = "王五" };

			//	Teacher t1 = new Teacher() { Name = "tom" };
			//	Teacher t2 = new Teacher() { Name = "joy" };
			//	Teacher t3 = new Teacher() { Name = "zck" };

			//	s1.Teachers.Add(t1);
			//	s1.Teachers.Add(t2);
			//	s1.Teachers.Add(t3);


			//	s2.Teachers.Add(t1);
			//	s2.Teachers.Add(t3);


			//	s3.Teachers.Add(t1);
			//	s3.Teachers.Add(t2);


			//	ctx.Teachers.Add(t1);
			//	ctx.Teachers.Add(t2);
			//	ctx.Teachers.Add(t3);

			//	ctx.Students.Add(s1);
			//	ctx.Students.Add(s2);
			//	ctx.Students.Add(s3);

			//	await ctx.SaveChangesAsync();
			//}

			using (MyDbContext ctx = new MyDbContext())
			{
				var teachers = ctx.Teachers.Include(t => t.Students);

				foreach (var teacher in teachers)
				{
					Console.WriteLine(teacher.Name);
					foreach (var s in teacher.Students)
					{
						Console.WriteLine("\t"+s.Name);
					}
				}
			}
		}
	}
}
